import { Inject, Injectable } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { SysUser } from './entities/sys-user.entity';
import { Repository } from 'typeorm';
import { Cron, CronExpression } from '@nestjs/schedule';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import { InjectQueue } from '@nestjs/bull';
import { Queue } from 'bull';
import { MailDto } from './dto/mail.dto';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(SysUser)
    private readonly sysUserRepository: Repository<SysUser>,
    @Inject(WINSTON_MODULE_NEST_PROVIDER)
    private readonly logger: Logger,
    @InjectQueue('emailSending')
    private readonly emailQueue: Queue,
  ) {}

  create(createUserDto: CreateUserDto) {
    return 'This action adds a new user';
  }

  async findAll(): Promise<SysUser[]> {
    return await this.sysUserRepository.find();
  }

  findOne(id: number) {
    return `This action returns a #${id} user`;
  }

  update(id: number, updateUserDto: UpdateUserDto) {
    return `This action updates a #${id} user`;
  }

  remove(id: number) {
    return `This action removes a #${id} user`;
  }

  @Cron(CronExpression.EVERY_30_SECONDS)
  handleCron() {
    console.log(new Date().getTime());
  }

  async sendEmail(data: MailDto) {
    const job = await this.emailQueue.add({
      data,
    });
    return {
      jobId: job.id,
    };
  }
  async sendWelcomeEmail(data: MailDto) {
    const job = await this.emailQueue.add('welcome', { data });
    return {
      jobId: job.id,
    };
  }

  async sendResetPasswordEmail(data: MailDto) {
    const job = await this.emailQueue.add('reset-password', { data });
    return {
      jobId: job.id,
    };
  }
}
